package com.feihong.yw189.activity.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.feihong.yw189.R;
import com.feihong.yw189.activity.MyActivityManager;
import com.feihong.yw189.activity.denglu.DengLuActivity;
import com.feihong.yw189.bluetooth.utils.util.ToastUtils;
import com.feihong.yw189.message.LoginMessage;
import com.feihong.yw189.model.User.UserEntity;
import com.feihong.yw189.utils.ScreenUtil;
import com.feihong.yw189.utils.UserLoginInfo;
import com.just.agentweb.AgentWeb;
import com.xiaomi.mistatistic.sdk.MiStatInterface;

import org.greenrobot.eventbus.EventBus;

import cn.pedant.SweetAlert.SweetAlertDialog;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * Created by Sabirjan on 2017/6/13.
 * TXGW.
 * 飞宏电子
 */

public abstract class  BaseActivity extends SupportActivity {
    public AgentWeb mAgentWeb;
    /**
     * 是否需要界面转移动画
     */
    protected boolean isTransAnim = true;

    /**
     * 状态栏字体颜色是否为深色
     */
    public boolean isDrakFont = true;

    /**
     * 状态栏颜色
     */
    public int mStatusColor = Color.TRANSPARENT;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme();
        windowStyle();
        super.onCreate(savedInstanceState);
        MyActivityManager.addActivity(this);
        setStatusFontColor();
    }
    protected void setStatusFontColor(){
        ScreenUtil.setStatusBarDarkFont(this,isDrakFont);
    }

    public void windowStyle(){
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();

            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(mStatusColor);

        }
    }
    public abstract void setTheme();

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams params = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            params.flags |= bits;
        } else {
            params.flags &= ~bits;
        }
        win.setAttributes(params);
    }


    @Override
    public void finish() {
        super.finish();
        if(isTransAnim){
            getActivity().overridePendingTransition(R.anim.fade_in_from_left, R.anim.slide_in_left);
        }
    }

    //显示登录提示
    public void showLoginDialog() {
        showConfirmDialog("未登录", "您还未登录，请先登录！", "登录", "取消",
                new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        Intent loginIntent = new Intent();
                        loginIntent.setClass(getActivity(), DengLuActivity.class);
                        startActivity(loginIntent);
                    }
                });
    }

    /**
     * 通用方法，判断用户是否登录，登录了打开页面，没有登录提示登录
     *
     * @param intent
     */
    public void checkLoginAndStartIntent(Intent intent) {
        if (UserLoginInfo.getUserLoginInfoInstance().isUserLogin()) {
            startActivity(intent);
        } else {
            showLoginDialog();
        }
    }

    public static final String TAG = "营维189";

    SweetAlertDialog pDialog = null;

    /**
     * 获取用户ID
     * 2017.7.25
     * 张鹏
     *
     * @return
     */
    public String getUserId() {
        UserEntity userInfo = UserLoginInfo.getUserLoginInfoInstance().getUserLoc();
        if (userInfo != null) {
            return userInfo.getUserID();
        } else {
            return null;
        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        if (isTransAnim) {
            getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        if (isTransAnim) {
            getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    }

    /**
     * 用户登录状态改变时通知接口 ， 在页面里面需要通知，则onCreate事件里面必须注册EventBus
     *
     * @param loginMessage
     */
    public abstract void onUserLogin(LoginMessage loginMessage);

    /**
     * 设置页面标题
     *
     * @param title
     */
    public void setPageTitle(String title) {
        if (title != null) {
            TextView textView = (TextView) findViewById(R.id.txt_pagetitle);
            if (textView != null) {
                textView.setText(title);
            }
        }

    }

    public void setBackButtonFinish() {
        RelativeLayout button = (RelativeLayout) findViewById(R.id.rl_goback);
        if (button != null) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    /**
     * 显示系统Toast
     *
     * @param message
     */
    public void showSystemToast(String message) {
        try {
            ToastUtils.showShortToast(this, message);
        } catch (Exception ex) {

        }

    }

    /**
     * 显示自定义Toast
     *
     * @param message
     */
    public void showToast(String message) {
        ToastUtils.showShortToast(this, message);
    }

    public void showInfoToast(String message) {
        ToastUtils.showShortToast(getActivity(), message);
    }

    public void showErrorToast(String message) {
        ToastUtils.showShortToast(getActivity(), message);
    }

    public Activity getActivity() {
        return this;
    }

    /***
     * 显示确认对话框
     * @param title  标题
     * @param message 内容
     * @param confirmText  确认按钮
     * @param cancelText  取消按钮
     * @param confirmClickListener 确认监听器
     */
    public void showConfirmDialog(String title, String message, String confirmText, String cancelText, SweetAlertDialog.OnSweetClickListener confirmClickListener) {
        showConfirmDialog(title, message, confirmText, cancelText, confirmClickListener, null);
    }

    public void showConfirmDialog(String title, String message, String confirmText, String cancelText, SweetAlertDialog.OnSweetClickListener confirmClickListener, SweetAlertDialog.OnSweetClickListener cancelClickListener) {
        SweetAlertDialog dialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.CUSTOM_IMAGE_TYPE);
        dialog.setTitleText(title)
                .setContentText(message)
                .setConfirmText(confirmText).setCancelText(cancelText);
        if (confirmClickListener != null) {
            dialog.setConfirmClickListener(confirmClickListener);
        }

        if (cancelClickListener != null) {
            dialog.setCancelClickListener(cancelClickListener);
        }


        dialog.show();
    }

    /**
     * 成功提示对话框
     *
     * @param title   标题
     * @param message 内容
     */
    public void showSuccessDialog(String title, String message) {
        showSuccessDialog(title, message, null);
    }

    public void showSuccessDialog(String title, String message, DialogInterface.OnDismissListener dismissListener) {
        showDialog(title, message, dismissListener, SweetAlertDialog.SUCCESS_TYPE);
    }

    public void showErrorDialog(String title, String message) {
        showErrorDialog(title, message, null);
    }

    public void showErrorDialog(String title, String message, DialogInterface.OnDismissListener dismissListener) {
        showDialog(title, message, dismissListener, SweetAlertDialog.ERROR_TYPE);
    }

    /**
     * 成功提示对话框
     *
     * @param title           标题
     * @param message         内容
     * @param dismissListener 对话框关闭监听器
     */
    public void showDialog(String title, String message, DialogInterface.OnDismissListener dismissListener, int type) {
        try {
            SweetAlertDialog dialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.CUSTOM_IMAGE_TYPE);
            dialog.setTitleText(title);
            dialog.setContentText(message);
            dialog.setCancelable(false);
            dialog.setNeutralText("知道了");
            if (dismissListener != null) {
                dialog.setOnDismissListener(dismissListener);
            }
            dialog.show();
        } catch (Exception ex) {

        }
    }


    /**
     * 等待对话框
     *
     * @param message 提示
     */
    public void showProgressDialog(String message) {
        showProgressDialog(message, false);
    }

    public void dismissProgressDialog() {
        if (pDialog != null) {
            if (pDialog.isShowing()) {
                pDialog.dismissWithAnimation();
            }
            pDialog = null;
        }
    }

    /**
     * 等待对话框
     *
     * @param message    提示
     * @param cancelable 是否可以返回键来关闭
     */
    public void showProgressDialog(String message, boolean cancelable) {
        if (pDialog != null) {
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
        }
        pDialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.PROGRESS_TYPE);
        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText(message);
        pDialog.setCancelable(cancelable);
        pDialog.show();
    }


    public boolean isUserLogin() {
        if (UserLoginInfo.getUserLoginInfoInstance().isUserLogin()) {
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {

        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        try {
            if (mAgentWeb != null) {
                mAgentWeb.destroy();
            }
            if (pDialog != null) {
                pDialog.dismiss();
            }
        } catch (Exception ex) {

        }
        dismissProgressDialog();
        MyActivityManager.removeActivity(this);
        super.onDestroy();
    }


    @Override
    protected void onResume() {
        super.onResume();
        MiStatInterface.recordPageStart(this, this.getClass().getName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        MiStatInterface.recordPageEnd();
    }

    /**
     * 界面是否销毁
     * @return
     */
    public boolean activityIsDestory() {
        if (isFinishing()) {
            return true;
        }
        if (isDestroyed()) {
            return true;
        }
        return false;
    }
}